<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
      <el-table-column label="头像" width="60" align="center"
                       :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-avatar :size="40" :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="120" header-align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="100" header-align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="phone" label="手机号码" width="120" align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="email" label="电子邮箱" width="150" header-align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="description" label="简介" header-align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="是否启用" width="100px" align="center">
        <template slot-scope="scope">
          <el-switch
              @change="updateEnable(scope.row)"
              v-model="scope.row.enable"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 以下是弹出的修改管理员对话窗 -->
    <el-dialog title="编辑管理员" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介" :label-width="formLabelWidth">
          <el-input v-model="editForm.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序序号" :label-width="formLabelWidth">
          <el-input v-model="editForm.sort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      editForm: {
        id: '',
        username: '',
        nickname: '',
        phone: '',
        email: '',
        description: '',
        enable: ''
      },
      formLabelWidth: '120px',
      enableStatus: [
        {url: '/disable', text: '禁用'},
        {url: '/enable', text: '启用'}],
    }
  },
  methods: {
    openEditDialog(admin){
      let url = 'http://localhost:30001/admins/'+ admin.id;
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000){
          this.editForm = responseBody.data;
          this.dialogFormVisible = true;
        } else if (responseBody.state == 40400){
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAdminList();
            }
          });
        }else {
          this.$message.error(responseBody.message);
        }
      })
    },
    submitEditForm(){
      let url = 'http://localhost:30001/admins/'+ this.editForm.id + '/update';
      let formData = this.qs.stringify(this.editForm);
      this.axios.post(url,formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000){
          this.$message({
            type: 'success',
            message: '修改管理员成功!'
          });
          this.loadAdminList();
          this.dialogFormVisible = false;
        }else if (responseBody.state == 40400){
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadAdminList();
            }
          });
        }else {
          this.$message.error(responseBody.message);
        }
      })
    },
    handleEdit(admin){
      console.log('尝试编辑【' + admin.name + '】，ID=' + admin.id);
    },
    openDeleteConfirm(admin) {
      this.$confirm('此操作将永久【' + admin.username + '】管理员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(admin);
      }).catch(() => {

      });
    },
    handleDelete(admin){
      let url = 'http://localhost:30001/admins/' + admin.id + '/delete';
      console.log(url);
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000){
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.loadAdminList();
        }else if (responseBody.state == 40400){
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAdminList();
            }
          });
        }else {
          this.$message.error(responseBody.message);
        }
      })
    },
    updateEnable(admin) {
      let url = 'http://localhost:30001/admins/'
          + admin.id + this.enableStatus[admin.enable].url;
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            type: 'success',
            message: this.enableStatus[admin.enable].text + '管理员成功!'
          });
          this.loadCategoryList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      })
    },
    loadAdminList() {
      let url = 'http://localhost:30001/admins';
      console.log(url);

      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        console.log('state=' + responseBody.state);
        console.log('message=' + responseBody.message);
        this.tableData = responseBody.data;
      })
    },
    
  },
  mounted() {
    console.log('mounted...');
    this.loadAdminList();
  }
}
</script>

<style scoped>

</style>